//
//  UIWindow+Extension.m
//  Shengzhuli
//
//  Created by FXGuo on 16/7/19.
//  Copyright © 2016年 kamangkeji. All rights reserved.
//

#import "UIWindow+Extension.h"
#import "SZLTabBarViewController.h"
#import "SZLGuideViewController.h"
#import "SZLAccountHelper.h"
#import "MBProgressHUD+MJ.h"
#import "ChatDemoHelper.h"
#import "ConVipViewViewController.h"

@interface UIWindow()<UITabBarControllerDelegate>


@end
@implementation UIWindow (Extension)

//登录以及切换rooter在这里面
- (void)switchRootViewController
{
    NSString *key = @"CFBundleShortVersionString";
    NSString *lastVersion = [[NSUserDefaults standardUserDefaults] objectForKey:key];
    NSString *currentVersion = [NSBundle mainBundle].infoDictionary[key];
    
    if ([currentVersion isEqualToString:lastVersion]) {
        [self setRooter];
    } else {
        self.rootViewController = [[SZLGuideViewController alloc]init];
        [[NSUserDefaults standardUserDefaults] setObject:currentVersion forKey:key];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(switchRooter:) name:SwitchRooter object:nil];
}
- (void)setRooter
{
    SZLTabBarViewController *tabVc = [[SZLTabBarViewController alloc]init];
    tabVc.delegate = self;
    self.rootViewController = tabVc;
    [ChatDemoHelper shareHelper].barVc = tabVc;
}
- (void)switchRooter:(NSNotification *)noti
{
    [self setRooter];
}
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
    if (tabBarController.childViewControllers[4] == viewController){
        return ![SZLAccountHelper shouldLogin];
    }
    return YES;
}

@end
